<%@page import="com.dilean.impl.model.ConversationImpl"%>
<%@page import="com.dilean.api.dao.ConversationDAO"%>
<%@page import="com.dilean.api.model.Conversation"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%-- imports --%>
<%@page import="com.dilean.api.dao.AttributeTypeDAO"%>
<%@page import="com.dilean.impl.model.AttributeTypeImpl"%>
<%@page import="com.dilean.api.model.AttributeType"%>
<%@page import="com.dilean.spring.AplicationContextHolder"%>
<%@page import="com.dilean.api.dao.PersonDAO"%>
<%@page import="com.dilean.impl.model.PersonImpl"%>
<%@page import="com.dilean.api.model.Person"%>
<%@page import="com.dilean.impl.model.AttributeListValueImpl"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueDate"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueList"%>
<%@page import="java.math.BigInteger"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueTextMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumber"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumberMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<%@page import="java.util.Collection"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.dilean.spring.AplicationContextHolder"%>
<%@page import="com.dilean.api.dao.PersonDAO"%>
<%@page import="com.dilean.impl.model.PersonImpl"%>
<%@page import="com.dilean.api.model.Person"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueListMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueList"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueDate"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumberMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueTextMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>

<!DOCTYPE html>
<html lang="en">
    <head>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">

        <!-- CSS and other resourses -->
        <link href="resources/mytheme/css/bootstrap.min.css" rel="stylesheet">

        <!-- Custom CSS -->
        <link href="resources/mytheme/css/simple-sidebar.css" rel="stylesheet">


        <link href="resources/mytheme/css/font-awesome.css" rel="stylesheet">

        <link href="resources/mytheme/css/font-awesome.min.css" rel="stylesheet">


        <%
            int friendId = Integer.parseInt(request.getParameter("id"));
            Person friend = new PersonImpl();
            friend = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithFriends(friendId);
            AttrValueText firstName = friend.getAttributeValue(1, AttrValueText.class);
            AttrValueText lastName = friend.getAttributeValue(2, AttrValueText.class);
            AttrValueTextMultiple emails = friend.getAttributeValue(3, AttrValueTextMultiple.class);
            AttrValueNumberMultiple telephone = friend.getAttributeValue(4, AttrValueNumberMultiple.class);
            AttrValueDate birthDate = friend.getAttributeValue(10, AttrValueDate.class);
            AttrValueText status = friend.getAttributeValue(5, AttrValueText.class);
            AttrValueDate regDate = friend.getAttributeValue(11, AttrValueDate.class);
            AttrValueList gender = friend.getAttributeValue(6, AttrValueList.class);
            AttrValueList relStatus = friend.getAttributeValue(7, AttrValueList.class);
            AttrValueList univercity = friend.getAttributeValue(9, AttrValueList.class);
            AttrValueListMultiple languages = friend.getAttributeValue(8, AttrValueListMultiple.class);

            Person me = new PersonImpl();
            int myId = 1;
            me = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithFriends(myId);
            Collection<Person> friendRequests = me.getFriendRequests();
            Collection<Person> friends = me.getFriends();
            int numberRequest = 0;
            for (Person fr : friendRequests) {
                if (friends.contains(fr) == false) {
                    numberRequest++;
                }
            }

            Collection<Person> userFriends = friend.getFriends();

            String fullname = "User";
            if ((firstName != null) && (lastName != null)) {
                fullname = firstName.getValue() + " " + lastName.getValue();
            } else if ((firstName == null) && (lastName != null)) {
                fullname = lastName.getValue();
            } else if ((firstName != null) && (lastName == null)) {
                fullname = firstName.getValue();
            }

            
            Person pers1 = new PersonImpl();
            Person pers2 = new PersonImpl();
            final int pers1Id = 1;
            final int pers2Id = Integer.parseInt(request.getParameter("id"));
            pers1 = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithConversations(pers1Id);
            pers2 = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithConversations(friendId);
            Collection<Conversation> conversations1 = pers1.getConversations();
            Collection<Conversation> conversations2 = pers2.getConversations();
            
             Conversation conversation = new ConversationImpl();
             conversation.setName("1_1");
                         int convId=0;

            
            for(Conversation c1:conversations1){
                Collection<Person> convPersons = AplicationContextHolder.getInstance().getContext().getBean(ConversationDAO.class).findByIdWithPersons(c1.getId()).getPersons();
                for(Conversation c2:conversations2){
                    if((c1.getId()==c2.getId())&&(convPersons.size()==2)){
                        
                        conversation=c1;
                        convId=c1.getId();
                    }
                }
            }
            
            
            
            if(conversation.getName().compareTo("1_1")==0){
                conversation.setName(pers1.getName()+" "+pers2.getName());
                conversation.addPerson(pers1);
                conversation.addPerson(pers2);
                convId=AplicationContextHolder.getInstance().getContext().getBean(ConversationDAO.class).save(conversation).getId();
            }
            
            
        %>     
        <title><%=fullname%> &middot; Dilean</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
    </head>
    <body>
        <div id="wrapper">

            <!-- Sidebar -->
            <div id="sidebar-wrapper">
                <<ul class="sidebar-nav">
                    <li class="sidebar-brand">
                        <a href="http://localhost:8080/Dilean/myPage.jsp">
                            <h2 align="centre" >Dilean</h2>
                        </a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/myPage.jsp"><i class="fa fa-home fa-fw-5x"></i>&nbsp; My Page</a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/friends.jsp?id=1"><i class="fa fa-users"></i>&nbsp; Friends </a>
                    </li>

                    <li>
                        <a href="http://localhost:8080/Dilean/allUsers.jsp?id=1"><i class="fa fa-database"></i>&nbsp; Users 
                            <%
                                if (numberRequest != 0) {
                            %>
                            <span class="badge badge-info"><%=numberRequest%> </span>
                            <%
                                }
                            %>
                        </a>
                    </li>



                    <li>
                        <a href="http://localhost:8080/Dilean/conversation.jsp"><i class="fa fa-envelope"></i>&nbsp; Messages </a>
                    </li>
                    <li class="divider"></li>
                    <li>
                        <a href="http://localhost:8080/Dilean/editUser.jsp?id=1"><i class="fa fa-cog"></i>&nbsp; Settings</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-book"></i>&nbsp; About</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-sign-out"></i>&nbsp; LogOut</a>
                    </li>

                </ul>
            </div>
            <!-- /#sidebar-wrapper -->

            <!-- Page Content -->
            <div id="page-content-wrapper">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-12">


                            <a class="btn btn-large btn-primary" href="#menu-toggle" id="menu-toggle" >


                                <i class="fa fa-user fa-3x"></i> <h4>User Menu</h4></a>

                            <h1 align="center">User Parameters</h1>
                            <div class="container" >
                                <div class="row">
                                    <div class="col-md-5  toppad  pull-right col-md-offset-3 ">

                                    </div>
                                    <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xs-offset-0 col-sm-offset-0 col-md-offset-3 col-lg-offset-3 toppad" >


                                        <div class="panel panel-info">
                                            <div class="panel-heading">
                                                <h2 class="panel-title" align="center"><%= fullname%></h2>
                                            </div>
                                            <div class="panel-body">
                                                <div class="row">
                                                    <div class="col-md-3 col-lg-3 " align="center"> <img alt="User Pic" src="https://lh5.googleusercontent.com/-b0-k99FZlyE/AAAAAAAAAAI/AAAAAAAAAAA/eu7opA4byxI/photo.jpg?sz=100" class="img-circle"> </div>


                                                    <div class=" col-md-9 col-lg-9 "> 
                                                        <table class="table table-user-information">
                                                            <tbody>
                                                                <%
                                                                    if (status != null) {
                                                                %>
                                                                <tr>
                                                                    <th><h5>Status</h5></th>
                                                            <th><h5><%= status.getValue()%></h5></th>
                                                            </tr>
                                                            <% }
                                                                if (emails != null) {
                                                            %>
                                                            <tr>
                                                                <td><h5>Email </h5></td><td>
                                                                    <%
                                                                        for (int i = 0; i < emails.getValue().size(); i++) {
                                                                            int j = i + 1;
                                                                            String emailStr = emails.getValue().get(i);
                                                                    %>
                                                                    <h5><a href="mailto:<%=emails.getValue().get(i)%>"><%= emails.getValue().get(i)%></a></h5>
                                                                        <%
                                                                            }
                                                                        %>
                                                                </td>
                                                            </tr> 
                                                            <%                                            }
                                                                if (telephone != null) {
                                                            %>
                                                            <tr>
                                                                <td><h5>Telephone number </h5></td><td>
                                                                    <%
                                                                        for (int i = 0; i < telephone.getValue().size(); i++) {
                                                                            int j = i + 1;
                                                                    %>
                                                                    <h5> <%= telephone.getValue().get(i)%></h5>
                                                                    <%
                                                                        }
                                                                    %>
                                                                </td>
                                                            </tr> 
                                                            <%                                            }
                                                                if (birthDate != null) {
                                                            %>
                                                            <tr>
                                                                <th><h5>Birth Data</h5></th>
                                                            <th><h5><%= birthDate.getDataStr()%></h5></th>
                                                            </tr>
                                                            <% }
                                                                if (regDate != null) {
                                                            %>
                                                            <tr>
                                                                <th><h5>Registration Data</h5></th>
                                                            <th><h5><%= regDate.getDataStr()%></h5></th>
                                                            </tr>
                                                            <% }
                                                                if (gender != null) {
                                                            %>
                                                            <tr>
                                                                <th><h5>Gender</h5></th>
                                                            <th><h5><%= gender.getValue().getName()%></h5></th>
                                                            </tr>
                                                            <% }
                                                                if (relStatus != null) {
                                                            %>
                                                            <tr>
                                                                <th><h5>Relationship Status</h5></th>
                                                            <th><h5><%= relStatus.getValue().getName()%></h5></th>
                                                                <% }
                                                                    if (univercity != null) {
                                                                %>
                                                            <tr>
                                                                <th><h5>Univercity</h5></th>
                                                            <th><h5><%= univercity.getValue().getName()%></h5></th>
                                                            </tr>
                                                            <% }
                                                                if (languages != null) {
                                                            %>
                                                            <tr>
                                                                <td><h5>Languages</h5></td><td>
                                                                    <%
                                                                        for (int i = 0; i < languages.getValue().size(); i++) {
                                                                            int j = i + 1;
                                                                    %>
                                                                    <h5><%= languages.getValue().get(i).getName()%></h5>
                                                                    <%
                                                                        }
                                                                    %>
                                                                </td>
                                                                <% }
                                                                %>
                                                            </tr>                                                               
                                                            </tbody>
                                                        </table>

                                                    </div>
                                                </div>
                                            </div>
                                            <div class="panel-footer">
                                                <div class="btn-group btn-group-justified">



                                                   <a class="btn btn-primary" href="http://localhost:8080/Dilean/chat.jsp?con=<%=convId %>"><i class="fa fa-envelope-o fa-fw"></i> Message</a>
                                                    <a class="btn btn-primary" href="http://localhost:8080/Dilean/friends.jsp?id=<%= friendId%>"><i class="fa fa-users"></i> Friends</a>

                                                    <%
                                                        if (friendId == myId) {
                                                    %>
                                                    <a href="http://localhost:8080/Dilean/myPage.jsp" class="btn btn-success"><i class="fa fa-home"></i>&nbsp; Me    </a>
                                                    <%
                                                    } else if (userFriends.contains(me) && friends.contains(friend)) {
                                                    %>
                                                    <a href="http://localhost:8080/Dilean/friendsRequestAction.jsp?id=<%=myId%>&par=4&frid=<%=friendId%>" class="btn btn-danger"><i class="fa fa-trash-o"></i>&nbsp; Delite</a>
                                                    <%
                                                    } else if (friends.contains(friend)) {
                                                    %>
                                                    <a href="http://localhost:8080/Dilean/friend.jsp?id=<%=friendId%>" class="btn btn-info"><i class="fa fa-user"></i>&nbsp; My Interesting Page    </a>
                                                    <%
                                                    } else if (friendRequests.contains(friend)) {
                                                    %>
                                                    <a href="http://localhost:8080/Dilean/friendsRequestAction.jsp?id=<%=myId%>&par=2&frid=<%=friendId%>" class="btn btn-success"><i class="fa fa-plus"></i>&nbsp; Add    </a>

                                                    <%
                                                    } else {
                                                    %>
                                                    <a href="http://localhost:8080/Dilean/friendsRequestAction.jsp?id=<%=myId%>&par=3&frid=<%=friendId%>" class="btn btn-success"><i class="fa fa-plus"></i>&nbsp; Add    </a>
                                                    <%

                                                        }


                                                    %>

                                                </div>
                                            </div>

                                        </div>
                                    </div>
                                </div>
                            </div>             
                        </div>
                        <!-- /#wrapper -->

                        <!-- jQuery Version 1.11.0 -->
                        <script src="resources/mytheme/js/jquery-1.11.0.js"></script>

                        <!-- Bootstrap Core JavaScript -->
                        <script src="resources/mytheme/js/bootstrap.min.js"></script>

                        <!-- Menu Toggle Script -->
                        <script>
                            $("#menu-toggle").click(function(e) {
                                e.preventDefault();
                                $("#wrapper").toggleClass("toggled");
                            });
                        </script>
                    </div>
                </div>
            </div>
        </div>                               
    </body>

</html>
